package com.common.util.utils;

import android.content.Context;
import android.os.Process;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @文件名称：SecurityManager.java
 * @文件作者：ljb
 * @文件描述：系统权限开放提醒即Root权限
 * @版本： 1.0.0
 * @创建时间：2017-12-19 09:18
 * @修改历史：2017-12-19 09:18
 */

public class SecurityManager {
    private final static String rootPath[] = {"/system/bin/", "/system/xbin/", "/system/sbin/", "/vendor/bin/"};
    private final static int RootState_Unknow = -1;
    private final static int RootState_Enable = 1;
    private final static int RootState_Disable = 0;
    private int rootState = RootState_Unknow;

    private static class LazyHolder {
        private static SecurityManager INSTANCE = new SecurityManager();
    }

    private SecurityManager() {}

    public static SecurityManager getManager() {
        return LazyHolder.INSTANCE;
    }

    public boolean isRootSystem() {
        if (rootState == RootState_Enable) {
            return true;
        } else if (rootState == RootState_Disable) {
            return false;
        }

        try {
            File file = null;
            for (int i = 0; i < rootPath.length; i++) {
                file = new File(rootPath[i] + "su");
                if (file != null && file.exists()) {
                    rootState = RootState_Enable;
                    return true;
                }
            }
        } catch (Exception e) {
        }

        rootState = RootState_Disable;
        return false;
    }

    /**
     * check /proc/self/maps 逐行读取，检测是否包含/data/data/pkgname(别的)，如果有就说明被注入了
     * @param context
     * @return
     */
    public boolean isXposedInjected(Context context) {
        String pkgName = context.getPackageName();
        int pid = Process.myPid();
        String mapsPath = "/proc/" + pid + "/maps";
        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(mapsPath)));
            String instring = null;
            while((instring = bufferedReader.readLine()) != null){
                instring = instring.trim();
                if (instring.length() != 0 && instring.contains("/data/data") && !instring.contains(pkgName)) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return false;
    }

    public void checkIsXposedInjected(Context context) {
        if (isRootSystem() && isXposedInjected(context)) {
            Toast.makeText(context, "检测到APP已被恶意软件控制，您的个人敏感信息可能会被窃取，请注意账户安全", Toast.LENGTH_SHORT).show();
        }
    }



}
